<%--
Copyright 2011 Michele Mancioppi [michele.mancioppi@gmail.com]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--%>
<%@ page language="java" contentType="application/xhtml+xml; charset=UTF-8" pageEncoding="UTF-8" import="com.google.appengine.api.utils.SystemProperty,java.util.*, javax.servlet.jsp.JspException,cave.nice.testMessage.data.*,com.google.appengine.api.users.*" %>
<%
String appid = SystemProperty.applicationId.get();

DataManager dataManager = DataManager.getInstance();
try {
	List<VerifiedAccount> verifiedAccounts = dataManager.getVerifiedAccounts();
	List<UnverifiedAccount> unverifiedAccounts = dataManager.getUnverifiedAccounts();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery.form.js"></script>
<script type="text/javascript" src="/js/jquery.validate.js"></script>
<script type="text/javascript" src="/js/additional-methods.js"></script>
<script type="text/javascript">
//prepare the form when the DOM is ready 
$(document).ready(function() {

	$("#sendmailConfigForm").validate({
		rules: {
			username: {
				required: true
			}
		},
		messages: {
			username: "Please enter a valid username"
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSend:		beforeFormSubmit,
				resetForm:		false, 
				type:			'post'
			});

			return false;
		}
	});

}); 

function beforeFormSubmit(jqXHR, settings) {
	var username = $("#username").val()
	var emailAddress = $("#emailAddress").val()

	download("/services/configuration/sendmail/.forward?username=" + username);
	download("/services/configuration/procmail/.procmailrc?emailAddress=" + emailAddress);

	return false;
}

function download(url) {
	window.open(url, "status=1");
}

function setupEmailAccounts() {
	return;
}
</script>
<title>How to setup your Email account to have it monitored by TestMessage</title>
</head>
<body>
<h1>How to setup your Email account to have it monitored by TestMessage</h1>
<p>At regular intervals, the TestMessage application sends to each monitored account an email containing a test. The email account must acknowledge the reception of the test, confirming to TestMessage that 'it is working' (or better, it does receive messages sent by TestMessage, which is enough to prove that the email account is 'not completely down').</p>
<p>This page explains how to setup your email account or email client to answer the tests.</p>
<h2>Answering or forwarding the test email</h2>
<p>You can answer the test by configuring your email account to automatically:</p>
<ul>
<li>answer the test email (N.B. make sure to include the original test email in the response!), or</li>
<li>forward the test email back to <a href="mailto:tests@<%= appid %>.appspotmail.com">tests@<%= appid %>.appspotmail.com</a>.</li>
</ul>
<p>Either options should work equally well. Just don't do both ;-)</p>
<h3><a name="#ForwardingFromSendmail">Setup a forwarding from Sendmail</a></h3>
<p>We all have a bit of <a ref="http://www.sendmail.org">Sendmail</a> in your lives (often unknowingly, and, if certain sys-admins are to be trusted, mostly unwillingly). A very neat way of configuring your email account for answering TestMessage tests is using the <a href="http://www.feep.net/sendmail/tutorial/intro/forward.html">.forward</a> file in conjunction with <a href="http://www.procmail.org/">procmail</a>. The idea is simple:</p>
<ol>
<li>Put a simple <emph>.forward</emph> file where Sendmail can process it (usually on the home folder of you account on the mail-server) that pipes incoming emails trough procmail</li>
<li>Instruct procmail through the <emph>.procmailrc</emph> file placed in your home folder on the mail-server to forward all incoming emails from tests@test-message.appspotmail.com back to tests@test-message.appspotmail.com (possibly without keeping a copy in your INBOX: your quota will be so proud of you!)</li>
</ol>
<p>If you are familiar with <emph>.forward</emph> and <emph>.procmailrc</emph>, it will take you a blink of an eye to setup everything. Otherwise, TestMessage can provide you with a version of these files that should (hopefully) work well in most configurations.</p>
<%
if(verifiedAccounts.isEmpty() && unverifiedAccounts.isEmpty()) {
%>
<p>However, it seems that you have currently no email accounts registered with TestMessage, verified or not. To have TestMessage generate the configuration files for you, you need (1) to be logged in your Google account, and (2) have registered at least one email address with TestMessage (email addresses do not necessarily have to be verified). To register an email address with TestMessage, go to the <a href="/registration/register.jsp">registration page</a>.</p>
<%
} else {
%>
<p>Just a few configuration parameters are needed, which you can fill-in using the form below. The configuration files provided by TestMail assume that no other <emph>.forward</emph> and <emph>.procmailrc</emph> is already in use.</p>
<p>You will have to download the two configuration files that TestMessage will create for you, and save/copy them on your home directory on the <a href="scp://zdi2.informatik.uni-stuttgart.de:22">ZDI mail-server (zdi2.informatik.uni-stuttgart.de)</a>. <b>N.B. The ZDI mail-server is not the SquirrelMail WebApp!</b>. To copy the files over to the ZDI mail-server, you may for example use <a href="http://winscp.net">WinSCP</a>. After you have copied the <emph>.forward</emph> and <emph>.procmailrc</emph> on your home on the ZDI mail-server, the test emails from TestMessage will automatically be forwarded back to TestMessage, without cluttering your IAAS inbox with unwanted stuff. The forwarding is virtually transparent, except for the <emph>procmail.log</emph> file that procmail will use to log what is going on (very important for troubleshooting!).</p>
<form id="sendmailConfigForm">
<fieldset>
<legend>Generate the configurations for IAAS's ZDI mail-server</legend>
<label for="username">Username on your mailserver:</label><input type="text" id="username" /><br />
<label for="emailAddress">Pick the email address you want to configure to work with TestMessage:</label><br />
<select id="emailAddress">
<%
for(VerifiedAccount account : verifiedAccounts) {
%>
<option value="<%= account.getEmailAddress() %>"><%= account.getEmailAddress() %> (verified)</option><br />
<%
}
for(UnverifiedAccount account : unverifiedAccounts) {
%>
<option value="<%= account.getEmailAddress() %>"><%= account.getEmailAddress() %> (not yet verified)</option><br />
<%
}
%>
</select><br />
<input type="submit" value="Download the config files"/><input type="reset" value="Reset this form"/>
</fieldset>
</form>
<%
}
%>
<h2>Answering a test using an HTTP request</h2>
<p>If setting up some SMTP forwarding is not your cup of coffee, but you are pretty handy with scripting and command-line HTTP clients like <a href="http://www.gnu.org/s/wget/">Wget</a>, then TestMessage has something that will fit you like a glove ;-) Each test email sent by TestMessage contains a URL like this:</p>
<ul>
<li>http://test-message.appspot.com/answerTest?test=[<a href="http://en.wikipedia.org/wiki/Universally_unique_identifier">UDDI</a>]</li>
</ul>
<p>By performing an HTTP GET request at that URL, you answer the test.</p>
</body>
</html>
<%
} finally {
	dataManager.close();
}
%>